(function (undefined){
    var trolley_obj = {};

    trolley_obj.config = {
        key: "trolley_id",
        classname: "trolley_add",
        delete_classname: "trolley_delete",
        center_form: $("#form1")
    }

    //购物车初始化
    trolley_obj.init = function(){
        $("."+trolley_obj.config.classname).each(function(){
            var id = this.id.substr(8);
            var obj = $(this);
            //得到 cookie            
            var cookie_value = jQuery.cookie(trolley_obj.config.key, undefined, {path:"/"});
            var pos = -1;
            if(cookie_value){
                pos = cookie_value.indexOf("," + id + ",");
            }

            if(pos > -1){
                //已经被加入购物车了
                obj.attr("class", "trolley_delete");
                $(this).html('<img width="16" height="16" title="点击移出购物车" alt="点击移出购物车" src="/static/skin/green/img/delete_shopping.gif">');
            }

            obj.bind("click", function(){
                var cur_class = $(this).attr("class");
                var id = this.id.substr(8);
                if(cur_class == "trolley_add"){
                    //加入购物车                    
                    trolley_obj.add_trolley(id);
                    $(this).attr("class", "trolley_delete");
                    $(this).html('<img width="16" height="16" title="点击移出购物车" alt="点击移出购物车" src="/static/skin/green/img/delete_shopping.gif">');
                }else{
                    //移出购物车
                    trolley_obj.delete_trolley(id);
                    $(this).attr("class", "trolley_add");
                    $(this).html('<img width="18" height="17" title="点击加入购物车" alt="点击加入购物车" src="/static/skin/green/img/add_shopping.gif">');
                }
            })

        })
    }


    //添加购物车操作
    trolley_obj.add_trolley = function(id){		
        if(isNaN(id)){
            alert("产品ID不正确");
            return false;
        }
        var cookie_value = jQuery.cookie(trolley_obj.config.key, undefined, {path:"/"});

        if(cookie_value){
            var pos = cookie_value.indexOf("," + id + ",");
            if(pos == -1){
                cookie_value += id + ",";
            }
        }else{
            cookie_value = "," + id + ",";
        }
        jQuery.cookie(trolley_obj.config.key, cookie_value, {path:"/"});

    }


    //移出购物车
    trolley_obj.delete_trolley = function(id){
        if(isNaN(id)){
            alert("产品ID不正确");
            return false;
        }

        //得到 cookie
        var cookie_value = jQuery.cookie(trolley_obj.config.key, undefined, {path:"/"});

        if(!cookie_value){
            return false;
        }

        cookie_value = cookie_value.replace(","+id+",", ",");
        jQuery.cookie(trolley_obj.config.key, cookie_value, {path:"/"});
    }

    //清空购物车
    trolley_obj.clear = function(){
        //清除 cookie
        jQuery.cookie(trolley_obj.config.key, null, {path:"/"});
        //修改记录后的按钮状态
        $("."+trolley_obj.config.delete_classname).each(function(){
            var id = this.id.substr(8);
            var obj = $(this);
            obj.attr("class", "trolley_add");
            obj.html('<img width="18" height="17" title="点击加入购物车" alt="点击加入购物车" src="/static/skin/green/img/add_shopping.gif">');
        })
    }


    if(window.trolley_obj == undefined){
        window.trolley_obj = trolley_obj;
    }
})();
